* Table D2: Winners, losers, and the number of assigned applicants: Greedy reject and combined algorithms

********************************************************************************
************************** Prepare data ****************************************
********************************************************************************

clear all
use contract_id_orig fulltime_orig funding_orig rank_of_admission_orig program_id_orig ///
	contract_id_h1 fulltime_h1 funding_h1 winner_h1 rank_of_admission_h1 program_id_h1 ///
	contract_id_h3 fulltime_h3 funding_h3 winner_h3 rank_of_admission_h3 program_id_h3 ///
	contract_id_h4 fulltime_h4 funding_h4 winner_h4 rank_of_admission_h4 program_id_h4 ///
	loser_h1 loser_h3 loser_h4 winner_h1 winner_h3 winner_h4 using "${data_matchings}", clear

********************************************************************************
************************** Prepare table ***************************************
********************************************************************************

cap file close sumstat
file open sumstat using "${tableD2}", write replace
file write sumstat "\begin{tabular}{l S[table-number-alignment = center, table-format = 5.0] S[table-number-alignment = center, table-format = 5.0] S[table-number-alignment = center, table-format = 5.0] S[table-number-alignment = center, table-format = 5.0]} \\ \hline\hline" _n
file write sumstat " & \multicolumn{1}{c}{Benchmark} & & \multicolumn{1}{c}{Greedy reject} & \multicolumn{1}{c}{Combined} \\ " _n
file write sumstat " & \multicolumn{1}{c}{SR-DA} & \multicolumn{1}{c}{SP-DA} & \multicolumn{1}{c}{algorithm} & \multicolumn{1}{c}{algorithm}  \\ " _n
file write sumstat " & \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)} & \multicolumn{1}{c}{(3)} & \multicolumn{1}{c}{(4)} \\ \hline "_n
file write sumstat "\multicolumn{5}{l}{A. {\it Number of assigned applicants}}\\ " _n
	* Number of students assigned ----------------------------------------------
	qui: count if contract_id_orig != .
	local benchmark_count: display %12.0fc r(N)
	
	foreach i in 1 3 4 {
		qui: count if contract_id_h`i' != .
		local h`i'_count: display %12.0fc r(N)
	}
	
	file write sumstat "Assigned to a contract & `benchmark_count' & `h4_count' & `h1_count' & `h3_count' \\ " _n

	* Assigned to a state-funded contract ---------------------------------------
	qui: count if contract_id_orig != . & funding_orig == 1
	local benchmark_count: display %12.0fc r(N)
	
	foreach i in 1 3 4 {
		qui: count if contract_id_h`i' != . & funding_h`i' == 1
		local h`i'_count: display %12.0fc r(N)
	}
	
	file write sumstat "Assigned to a state-funded contract & `benchmark_count' & `h4_count' & `h1_count' & `h3_count' \\ " _n
	
	* Assigned to self-funded contract -----------------------------------------
	qui: count if contract_id_orig != . & funding_orig == 2
	local benchmark_count: display %12.0fc r(N)
	
	foreach i in 1 3 4 {
		qui: count if contract_id_h`i' != . & funding_h`i' == 2
		local h`i'_count: display %12.0fc r(N)
	}
	
	file write sumstat "Assigned to a self-funded contract & `benchmark_count' & `h4_count' & `h1_count' & `h3_count' \\ " _n
	
	* Assigned to self-funded w/ full time contract ----------------------------
	qui: count if contract_id_orig != . & funding_orig == 2 & fulltime_orig == 1
	local benchmark_count: display %12.0fc r(N)
	
	foreach i in 1 3 4 {
		qui: count if contract_id_h`i' != . & funding_h`i' == 2 & fulltime_h`i' == 1
		local h`i'_count: display %12.0fc r(N)
	}
	
	file write sumstat "Assigned to a self-funded, full-time contract & `benchmark_count' & `h4_count' & `h1_count' & `h3_count'  \\ " _n
	
	
	* Winners ------------------------------------------------------------------
	file write sumstat "\multicolumn{5}{l}{}\\ " _n
	
	foreach i in 1 3 4 {
		qui: count if winner_h`i' == 1
		local w`i':  display %12.0fc r(N)
	
	}
	file write sumstat "B. {\it Winners} & & `w4'& `w1' & `w3'  \\ " _n
	
	foreach i in 1 3 4 {
		qui: count if winner_h`i' == 1 & rank_of_admission_orig == .
		local w`i':  display %12.0fc r(N)
	
	}
	file write sumstat " -- Newly assigned &  & `w4'& `w1' & `w3'  \\ " _n
	
	foreach i in 1 3 4 {
		qui: count if winner_h`i' == 1 & rank_of_admission_orig != . & program_id_h`i' != program_id_orig
		local w`i':  display %12.0fc r(N)
	
	}
	file write sumstat " -- New program & & `w4' & `w1' & `w3'  \\ " _n
	
	foreach i in 1 3 4 {
		qui: count if winner_h`i' == 1 & rank_of_admission_orig != . & program_id_h`i' == program_id_orig
		local w`i':  display %12.0fc r(N)
	
	}
	file write sumstat " -- Same program, preferred financial terms & & `w4' & `w1' & `w3'  \\ " _n
			
	* Losers ------------------------------------------------------------------
	file write sumstat "\multicolumn{5}{l}{}\\ " _n
	
	foreach i in 1 3 4 {
		qui: count if loser_h`i' == 1
		local l`i':  display %12.0fc r(N)
	
	}
	file write sumstat "C. {\it Losers} &  & `l4' & `l1' & `l3'   \\ " _n
	
	foreach i in 1 3 4 {
		qui:count if loser_h`i' == 1 & rank_of_admission_h`i' == .
		local l`i':  display %12.0fc r(N)
	
	}
	file write sumstat " -- Newly unassigned & & `l4' & `l1' & `l3'  \\ " _n
	
	foreach i in 1 3 4 {
		qui: count if loser_h`i' == 1 & rank_of_admission_h`i' != . & program_id_h`i' != program_id_orig
		local l`i':  display %12.0fc r(N)
	
	}
	file write sumstat " -- New program & & `l4' & `l1' & `l3'  \\ " _n
	
	foreach i in 1 3 4 {
		qui: count if loser_h`i' == 1 & rank_of_admission_h`i' != . & program_id_h`i' == program_id_orig
		local l`i':  display %12.0fc r(N)
	
	}
	file write sumstat " -- Same program, less preferred financial terms &   & `l4' & `l1' & `l3' \\ \hline\hline" _n
	file write sumstat "\multicolumn{5}{p{15.5cm}}{{\it Notes}: The table presents the number of assigned applicants under various stable assignments (Panel A). Panels B and C describe the number of winners and losers from changing the benchmark \SRDA to \SPDA and to our alternate algorithms. An applicant is a winner if she is assigned to a contract she ranked higher than her contract in the benchmark assignment. An applicant is a loser if she is assigned to a contract she ranked lower than her assigned contract in the benchmark assignment or if she becomes unassigned. Column (1) shows the benchmark assignment. Column (2) presents the \SPDA assignment. Column (3) presents the greedy reject algorithm assignment and column (4) presents the combined algorithm assignment.} \\ " _n
file write sumstat "\end{tabular}" _n
file close sumstat
